<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>第一节课</title>
		<style type="text/css">
			/*去除原来样式*/
			
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				/*
				 * 将边框设置在中间
				 */
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 500px;
				height: 500px;
				border: 1px solid #333333;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="500" height="500"></canvas>
		</div>
	</body>

	<script>
		
		window.onload = function() {
			//let oCanvas = document.getElementById('canvas');
			let oCanvas = document.getElementById("canvas");
			let pen = oCanvas.getContext("2d");

			pen.fillStyle = "lightcoral";
			
			//本来是沿着坐标轴原点旋转
			/*pen.fillRect(200,0,100,50);
			pen.rotate(Math.PI/180*45);
			pen.fillRect(200,0,100,50);*/
			
			//设置沿着中心旋转
			//参数：X坐标位置，Y坐标位置，X轴方向长度，Y轴方向长度，旋转角度，变换后颜色(默认原色)
			
			function spin(x,y,w,h,angle,color){
				//移动的是原点坐标（旋转中心）
				pen.translate(x+w/2,y+h/2);
				pen.rotate(Math.PI/180*angle)
				pen.fillStyle=color;
				pen.translate(-(x+w/2),-(y+h/2));
				pen.fillRect(x,y,w,h);
			}
			
			//实例
			pen.fillRect(200,150,100,200);
			spin(200,150,100,200,90,"red");
			
			
		}
	</script>

</html>